package it.zqdszb.ui.fragment;

import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.v7.widget.GridLayoutManager;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import java.util.Iterator;
import java.util.List;

import butterknife.BindView;
import it.zqdszb.R;
import it.zqdszb.entity.ChannelListBean;
import it.zqdszb.library.retrofit.exception.ApiException;
import it.zqdszb.library.ui.BaseLazyMVPFragment;
import it.zqdszb.library.view.EmptyLayout;
import it.zqdszb.mvp.contract.ChannelListContract;
import it.zqdszb.mvp.modelImp.ChannelListModelModelImp;
import it.zqdszb.mvp.presenter.ChannelListPresenter;
import it.zqdszb.ui.adapter.ChannelListAdapter;

/**
 * Created by GH on 2018-05-14.
 */

public class ChannelListFragment extends BaseLazyMVPFragment <ChannelListPresenter,ChannelListModelModelImp>
		implements ChannelListContract.ChannelListView {

	public int groupId;
	@BindView(R.id.emptyLayout)
	EmptyLayout emptyLayout;

	@BindView(R.id.loadingView)
	View loadingView;

	public ChannelListFragment newInstance(String tabName, int groupId) {
		ChannelListFragment fragment = new ChannelListFragment();
		Bundle b = new Bundle();
		b.putSerializable("PagerTitle", tabName);
		b.putInt("groupId", groupId);
		fragment.setArguments(b);
		return fragment;
	}

	@Nullable
	@Override
	public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
		return setView(inflater, R.layout.fragment_channel_list);
	}

	@Override
	protected void initView() {
		groupId=getArguments().getInt("groupId");
		ChannelListAdapter channelListAdapter=new ChannelListAdapter(R.layout.item_channel_list,datas,getThisActivity());
		emptyLayout.getListView().setLayoutManager(new GridLayoutManager(getThisActivity(),3));
		emptyLayout.setAdapter(channelListAdapter);
		presenter.getData(groupId);
	}

	@Override
	public void showView(List<ChannelListBean> result) {
		Iterator<ChannelListBean> iter = result.iterator();
		while(iter.hasNext()){
			ChannelListBean tabBean = iter.next();
			if(tabBean.getCibn()==1){
				iter.remove();
			}
		}
		emptyLayout.showView();
		loadingView.setVisibility(View.GONE);
		datas.clear();
		datas.addAll(result);
		emptyLayout.getAdapter().notifyDataSetChanged();
	}

	@Override
	public void showError(ApiException e) {

	}
}
